package com.jgy.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 姜光远
 * @date 2023/7/6 9:48
 */
public class TimePeriod {
    private String startTime;
    private String endTime;

    public TimePeriod(String startTime, String endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public List<Map<String, String>> getShortestPeriods(String type) {
        List<Map<String, String>> shortestPeriods = new ArrayList<>();

        LocalDate startDate = parseDate(startTime);
        LocalDate endDate = parseDate(endTime);

        if (startDate.isAfter(endDate)) {
            LocalDate temp = startDate;
            startDate = endDate;
            endDate = temp;
        }

        if ("yyyy-MM".equals(type)) {
            while (!startDate.isAfter(endDate)) {
                Map<String, String> period = new HashMap<>();
                period.put("name", formatDateString(startDate, "yyyy-MM"));

                shortestPeriods.add(period);
                startDate = startDate.plusMonths(1);
            }
        } else if ("yyyy-MM-dd".equals(type)) {
            while (!startDate.isAfter(endDate)) {
                Map<String, String> period = new HashMap<>();
                period.put("name", formatDateString(startDate, "yyyy-MM-dd"));

                shortestPeriods.add(period);
                startDate = startDate.plusDays(1);
            }
        } else if ("yyyy-MM-dd HH:mm:ss".equals(type)) {
            LocalDateTime startDateTime = parseDateTime(startTime);
            LocalDateTime endDateTime = parseDateTime(endTime);

            while (!startDateTime.isAfter(endDateTime)) {
                Map<String, String> period = new HashMap<>();
                period.put("name", formatDateString(startDateTime, "yyyy-MM-dd HH:mm:ss"));

                shortestPeriods.add(period);
                startDateTime = startDateTime.plusHours(1);
            }
        }

        return shortestPeriods;
    }

    private LocalDate parseDate(String date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDate.parse(date, formatter);
    }

    private LocalDateTime parseDateTime(String dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateTime, formatter);
    }

    private String formatDateString(LocalDate date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return date.format(formatter);
    }

    private String formatDateString(LocalDateTime dateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }

    public static void main(String[] args) {
        TimePeriod timePeriod = new TimePeriod("2023-06-01 12:00:00", "2023-06-01 15:00:00");
        List<Map<String, String>> shortestPeriods = timePeriod.getShortestPeriods("yyyy-MM-dd HH:mm:ss");

        for (Map<String, String> period : shortestPeriods) {
            System.out.println(period.get("name"));
        }
    }
}